package com.newegg.app.qec.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newegg.app.qec.R;
import com.newegg.app.qec.activity.AccountActivity;
import com.newegg.app.qec.activity.AddressActivity;
import com.newegg.app.qec.activity.LoginActivity;
import com.newegg.app.qec.activity.PersonInfoActivity;
import com.newegg.app.qec.activity.RegisterActivity;
import com.newegg.app.qec.activity.SetActivity;
import com.newegg.app.qec.util.Constants;

import java.io.ByteArrayInputStream;


/**
 * Created by Allen on 2017/12/6.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    private LinearLayout login_linear;
    private TextView username_tv;
    //记录当前页面是否打开过
    private boolean boo = false;
    private ImageView head_picture;
    private LinearLayout mine_address;
    private LinearLayout mine_account;
    private LinearLayout mine_set;


    /**
     * 用来接收更新数据
     *
     * @param i
     */
    public void MineFragment(int i) {
        //如果页面没有打开过，说明控件也没有初始化过，不打开执行该操作
        if (boo) {
            //如果本地用户数据不为空，就显示用户信息
            boolean loginState = Constants.getLoginByName(getActivity(), "LoginState");
            //i=0  获取数据    i=1 取消数据
            if (i == 0) {
                if (loginState) {
                    login_linear.setVisibility(View.GONE);
                    username_tv.setVisibility(View.VISIBLE);
                    head_picture.setClickable(true);
                    //取出头像
                    String photo = Constants.getHeadPhotoByName(getActivity(), "photo");
                    //判断是否设置有头像
                    if (photo != null && photo.length() != 0 && !photo.equals("")) {
                        ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(photo.getBytes(), Base64.DEFAULT));
                        Drawable drawable = Drawable.createFromStream(bais, "");
                        head_picture.setImageDrawable(drawable);
                    } else {
                        head_picture.setImageResource(R.mipmap.head_picture);
                    }

                } else {
                    //为空就提示登陆
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            } else {
                //退出后显示
                login_linear.setVisibility(View.VISIBLE);
                username_tv.setVisibility(View.GONE);
                head_picture.setClickable(false);
                head_picture.setImageResource(R.mipmap.head_picture);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        boo = true;

        TextView login_tv = (TextView) view.findViewById(R.id.login_tv);
        TextView register_tv = (TextView) view.findViewById(R.id.register_tv);
        login_linear = (LinearLayout) view.findViewById(R.id.login_linear);
        username_tv = (TextView) view.findViewById(R.id.username_tv);
        head_picture = (ImageView) view.findViewById(R.id.head_picture);
        mine_address = (LinearLayout) view.findViewById(R.id.mine_address);
        mine_account = (LinearLayout) view.findViewById(R.id.mine_account);
        mine_set = (LinearLayout) view.findViewById(R.id.mine_set);

        //判断是否登录
        if (Constants.getLoginByName(getActivity(), "LoginState")) {
            login_linear.setVisibility(View.GONE);
            username_tv.setVisibility(View.VISIBLE);
            head_picture.setClickable(true);
            //取出头像
            String photo = Constants.getHeadPhotoByName(getActivity(), "photo");
            //判断是否设置有头像
            if (photo != null && photo.length() != 0 && !photo.equals("")) {
                ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(photo.getBytes(), Base64.DEFAULT));
                Drawable drawable = Drawable.createFromStream(bais, "");
                head_picture.setImageDrawable(drawable);
            } else {
                head_picture.setImageResource(R.mipmap.head_picture);
            }
        } else {
            login_linear.setVisibility(View.VISIBLE);
            username_tv.setVisibility(View.GONE);
            head_picture.setClickable(false);
        }

        login_tv.setOnClickListener(this);
        register_tv.setOnClickListener(this);
        head_picture.setOnClickListener(this);
        mine_address.setOnClickListener(this);
        mine_account.setOnClickListener(this);
        mine_set.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.login_tv:
                intent.setClass(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.register_tv:
                intent.setClass(getActivity(), RegisterActivity.class);
                intent.putExtra("tag", "register");
                startActivity(intent);
                break;
            case R.id.head_picture:
                if (Constants.getLoginByName(getActivity(), "LoginState")) {
                    intent.setClass(getActivity(), PersonInfoActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), LoginActivity.class);
                }
                break;
            case R.id.mine_address:
                intent.setClass(getActivity(), AddressActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_account:
                intent.setClass(getActivity(), AccountActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_set:
                intent.setClass(getActivity(), SetActivity.class);
                startActivity(intent);
                break;
        }
    }
}
